package com.sina.microblog.ut.cases
{
	import com.sina.microblog.MicroBlog;
	import com.sina.microblog.events.MicroBlogErrorEvent;
	import com.sina.microblog.events.MicroBlogEvent;
	import com.sina.microblog.ut.utils.AssertResult;
	import com.sina.microblog.ut.utils.ResultDataUtils;
	import com.sina.microblog.ut.utils.URLRequestChecker;
	import com.sina.microblog.ut.utils.URLRequestParameters;
	import com.ut.nm_test_internal;
	
	import flash.net.URLRequest;
	import flash.net.URLRequestMethod;
	
	import org.flexunit.asserts.assertNull;
	import org.flexunit.async.Async;
	
	use namespace nm_test_internal;
	
	[RunWith("org.flexunit.experimental.theories.Theories")]
	public class FavoritesTest
	{
		private static const FAVORITES_LIST_URL_BASE:String = "http://api.t.sina.com.cn/favorites.xml";
		private static const FAVORITES_CREATE_URL_BASE:String = "http://api.t.sina.com.cn/favorites/create.xml";
		private static const FAVORITES_DESTROY_URL_BASE:String = "http://api.t.sina.com.cn/favorites/destroy/";
		
		private static const TIME_OUT:uint = 500;	
		private static var mb:MicroBlog;
		private static var testMessageID:uint = 132323;
		
		[DataPoints]
		[ArrayElementType("uint")]
		public static var ids:Array = [ 0, 1, 201, 300];
		
		public function FavoritesTest()
		{
		}
		[BeforeClass]
		public static function setUp():void
		{
			if ( null == mb )
			{
				mb = new MicroBlog();
			}
		}
		//Test Favorites.
		[Test(async,timeout="20000")]
		public function testFavorites():void
		{
			Async.handleEvent(this, mb, MicroBlogEvent.LOAD_FAVORITE_LIST_RESULT, AssertResult.assertStatusArray, TIME_OUT);
			var params:URLRequestParameters = new URLRequestParameters(mb.source);
			var url:String = FAVORITES_LIST_URL_BASE + params.toGetString();
			
			var req:URLRequest = new URLRequest(url);
			mb.loadFavoriteList();
			URLRequestChecker.check(mb, req, ResultDataUtils.statusArray);			
			
		}
		[Test(async,timeout="20000")]
		public function testFavoritesFail():void
		{
			Async.handleEvent(this, mb, MicroBlogErrorEvent.LOAD_FAVORITE_LIST_ERROR, AssertResult.assertError, TIME_OUT);
			var params:URLRequestParameters = new URLRequestParameters(mb.source);
			params.addParam("page", 3);
			var url:String = FAVORITES_LIST_URL_BASE + params.toGetString();
			
			var req:URLRequest = new URLRequest(url);
			mb.loadFavoriteList(3);
			URLRequestChecker.check(mb, req, ResultDataUtils.error);
		}
		[Theory]
		public function testFavoritesTheory(page:uint):void
		{
			var params:URLRequestParameters = new URLRequestParameters(mb.source);
			if ( page > 0 )
			{
				params.addParam("page", page);
			}
			var url:String = FAVORITES_LIST_URL_BASE + params.toGetString();
			
			var req:URLRequest = new URLRequest(url);
			mb.loadFavoriteList(page);
			URLRequestChecker.check(mb, req, null);
		}
		
		//Test Favorites Create.
		[Test(async,timeout="20000")]
		public function testFavoritesCreate():void
		{
			Async.handleEvent(this, mb, MicroBlogEvent.ADD_TO_FAVORITES_RESULT, AssertResult.assertStatus, TIME_OUT);
			
			var params:URLRequestParameters = new URLRequestParameters(mb.source);
			params.addParam("id", testMessageID);
			
			var url:String = FAVORITES_CREATE_URL_BASE;
			var req:URLRequest = new URLRequest(url);
			req.method = URLRequestMethod.POST;
			req.data = params.toPostData();
			mb.addToFavorites(testMessageID);
			URLRequestChecker.check(mb, req, ResultDataUtils.status);			
			
		}
		[Test(async,timeout="20000")]
		public function testFavoritesCreateFail():void
		{
			Async.handleEvent(this, mb, MicroBlogErrorEvent.ADD_TO_FAVORITES_ERROR, AssertResult.assertError, TIME_OUT);
			var params:URLRequestParameters = new URLRequestParameters(mb.source);
			params.addParam("id", testMessageID);
			var url:String = FAVORITES_CREATE_URL_BASE;
			
			var req:URLRequest = new URLRequest(url);
			req.method = URLRequestMethod.POST;
			req.data = params.toPostData();
			
			mb.addToFavorites(testMessageID);
			URLRequestChecker.check(mb, req, ResultDataUtils.error);
		}
		[Test]
		public function testFavoritesCreateWithZeroID():void
		{
			mb.addToFavorites(0);
			assertNull(mb.lastRequest);	
			
		}
		//Test Favorites Destroy.
		[Test(async,timeout="20000")]
		public function testFavoritesDestroy():void
		{
			Async.handleEvent(this, mb, MicroBlogEvent.REMOVE_FROM_FAVORITES_RESULT, AssertResult.assertStatus, TIME_OUT);
			
			var params:URLRequestParameters = new URLRequestParameters(mb.source);
			
			var url:String = FAVORITES_DESTROY_URL_BASE + testMessageID.toString() + ".xml";
			var req:URLRequest = new URLRequest(url);
			req.method = URLRequestMethod.POST;
			req.data = params.toPostData();
			mb.removeFromFavorites(testMessageID);
			URLRequestChecker.check(mb, req, ResultDataUtils.status);			
			
		}
		[Test(async,timeout="20000")]
		public function testFavoritesDestroyFail():void
		{
			Async.handleEvent(this, mb, MicroBlogErrorEvent.REMOVE_FROM_FAVORITES_ERROR, AssertResult.assertError, TIME_OUT);
			var params:URLRequestParameters = new URLRequestParameters(mb.source);
			
			var url:String = FAVORITES_DESTROY_URL_BASE + testMessageID.toString() + ".xml";
			
			var req:URLRequest = new URLRequest(url);
			req.method = URLRequestMethod.POST;
			req.data = params.toPostData();
			
			mb.removeFromFavorites(testMessageID);
			URLRequestChecker.check(mb, req, ResultDataUtils.error);
		} 
		[Test]
		public function testFavoritesDestroyWithZeroID():void
		{
			mb.removeFromFavorites(0); 
			assertNull(mb.lastRequest);	
			
		}
	}
}